/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.alarm.overview;

import java.awt.Graphics;

import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;


import twaver.Alarm;
import twaver.AlarmSeverity;
import twaver.AlarmTrendIndication;
import twaver.TWaverUtil;
import twaver.swing.LabelValueLayout;
 
public class AlarmUIBasicPane extends JPanel {
    private class TrendIndicationTextField extends JTextField {
        private AlarmTrendIndication trend = null;

        public void setAlarmTrendIndication(AlarmTrendIndication trend) {
            this.trend = trend;
            this.repaint();
        }

        public void setText(String text) {
            super.setText("");
        }

        public void paintComponent(Graphics g) {
            super.paintComponent(g);

            if (trend != null) {
                Icon icon = null;
                if (trend == AlarmTrendIndication.LESS_SEVERE) {
                    icon = TWaverUtil.getIcon("/resource/image/direction/se.png");
                }
                if (trend == AlarmTrendIndication.NO_CHANGE) {
                    icon = TWaverUtil.getIcon("/resource/image/direction/e.png");
                }
                if (trend == AlarmTrendIndication.MORE_SEVERE) {
                    icon = TWaverUtil.getIcon("/resource/image/direction/ne.png");
                }
                int y = (this.getHeight() - icon.getIconHeight()) / 2;
                int x = 16; //(this.getWidth() - icon.getIconWidth()) / 2;
                icon.paintIcon(null, g, x, y);
            }
        }
    }

	private class SeverityTextField extends JTextField {
		public void setAlarmSeverity(AlarmSeverity severity) {
			if (severity != null) {
				this.setText(severity.getName());
				this.setBackground(severity.getColor());
			} else {
				this.setText("");
			}
			this.repaint();
		}
	}

    LabelValueLayout layout = new LabelValueLayout(10, 2);
    JLabel lbAlarmID = new JLabel();
    JTextField txtAlarmID = new JTextField();
    JLabel lbElement = new JLabel();
    JTextField txtElementID = new JTextField();
    JLabel lbAlarmSeverity = new JLabel();
    SeverityTextField txtAlarmSeverity = new SeverityTextField();
    JLabel lbAlarmType = new JLabel();
    JTextField txtAlarmType = new JTextField();
    JLabel lbTrendIndication = new JLabel();
    TrendIndicationTextField txtTrendIndication = new TrendIndicationTextField();

	public AlarmUIBasicPane() {
		init();
	}

    private void init() {
        lbAlarmID.setText("Alarm ID:");
        this.setLayout(layout);
        lbElement.setText("Source element ID:");
        lbAlarmSeverity.setText("Alarm severity:");
        lbAlarmType.setText("Alarm type:");
        lbTrendIndication.setText("Trend indication:");
        txtElementID.setEditable(false);
        txtElementID.setText("");
        txtAlarmID.setEditable(false);
        txtAlarmID.setText("");
        txtAlarmSeverity.setEditable(false);
        txtAlarmSeverity.setText("");
        txtAlarmType.setEditable(false);
        txtAlarmType.setText("");
        txtTrendIndication.setEditable(false);
        txtTrendIndication.setText("");
        this.add(lbAlarmID, null);
        this.add(txtAlarmID, null);
        this.add(lbElement, null);
        this.add(txtElementID, null);
        this.add(lbAlarmSeverity, null);
        this.add(txtAlarmSeverity, null);
        this.add(lbAlarmType, null);
        this.add(txtAlarmType, null);
        this.add(lbTrendIndication, null);
        this.add(txtTrendIndication, null);
    }

    public void setAlarm(Alarm alarm) {
        reset();
        this.txtAlarmID.setText(alarm.getAlarmID() == null ? "" : alarm.getAlarmID().toString());
        this.txtElementID.setText(alarm.getElementID() == null ? "" : alarm.getElementID() .toString());
        this.txtAlarmSeverity.setAlarmSeverity(alarm.getAlarmSeverity());
        this.txtAlarmType.setText(alarm.getAlarmType() == null ? "" : alarm.getAlarmType().getName());
        this.txtTrendIndication.setAlarmTrendIndication(alarm.getTrendIndication());
    }

    private void reset() {
        this.txtAlarmID.setText("");
        this.txtElementID.setText("");
        this.txtAlarmSeverity.setAlarmSeverity(null);
        this.txtAlarmType.setText("");
        this.txtTrendIndication.setAlarmTrendIndication(null);
    }
}